﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace InfoJetSoft.Service.Util
{
    public class QuickSelectUtil
    {
        //CID 0996 //2011.08.13 //Begin
        public static XmlElement SelectChildElementByLocalName(XmlElement element, string localName)
        {
            foreach (XmlNode node in element.ChildNodes)
            {
                if (node is XmlElement)
                {
                    XmlElement currentElement = (XmlElement)node;
                    if (currentElement.LocalName.Equals(localName, StringComparison.Ordinal))
                    {
                        return currentElement;
                    }
                }
            }
            return null;
        }
        //CID 0996 //2011.08.13 //End

        //CID 0996 //2011.08.13 //Begin
        public static List<XmlElement> SelectChildElementsByLocalName(XmlElement element, string localName)
        {
            List<XmlElement> elements = new List<XmlElement>();
            foreach (XmlNode node in element.ChildNodes)
            {
                if (node is XmlElement)
                {
                    XmlElement currentElement = (XmlElement)node;
                    if (currentElement.LocalName.Equals(localName, StringComparison.Ordinal))
                    {
                        elements.Add(currentElement);
                    }
                }
            }
            return elements;
        }
        //CID 0996 //2011.08.13 //End

        //CID 0996 //2011.07.28 //Begin
        public static XmlElement SelectChildElementByLocalNameSteps(XmlElement element, string[] localNames)
        {
            List<XmlElement> parentElements = new List<XmlElement>();
            parentElements.Add(element);
            int i = 0;
            for (i = 0; i < localNames.Length; i++)
            {
                string localName = localNames[i];
                List<XmlElement> foundElements = new List<XmlElement>();
                foreach (XmlElement parentElement in parentElements)
                {
                    foreach (XmlNode node in parentElement.ChildNodes)
                    {
                        if (node is XmlElement)
                        {
                            XmlElement currentElement = (XmlElement)node;
                            if (currentElement.LocalName.Equals(localName, StringComparison.Ordinal))
                            {
                                foundElements.Add(currentElement);
                            }
                        }
                    }
                }
                if (i == (localNames.Length - 1))
                {
                    if (foundElements.Count > 0)
                    {
                        return foundElements[0];
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    parentElements = foundElements;
                }
            }
            return null;
        }
        //CID 0996 //2011.08.12 //End

        //CID 0996 //2011.07.29 //Begin
        public static List<XmlElement> SelectChildElementsByLocalNameSteps(XmlElement element, string[] localNames)
        {
            List<XmlElement> parentElements = new List<XmlElement>();
            parentElements.Add(element);
            int i = 0;
            for (i = 0; i < localNames.Length; i++)
            {
                string localName = localNames[i];
                List<XmlElement> foundElements = new List<XmlElement>();
                foreach (XmlElement parentElement in parentElements)
                {
                    foreach (XmlNode node in parentElement.ChildNodes)
                    {
                        if (node is XmlElement)
                        {
                            XmlElement currentElement = (XmlElement)node;
                            if (currentElement.LocalName.Equals(localName, StringComparison.Ordinal))
                            {
                                foundElements.Add(currentElement);
                            }
                        }
                    }
                }
                if (i == (localNames.Length - 1))
                {
                    return foundElements;
                }
                else
                {
                    parentElements = foundElements;
                }
            }
            return null;
        }
        //CID 0996 //2011.07.29 //End

        //CID 0996 //2011.07.29 //Begin
        public static XmlElement SelectChildElementByLocalNameStepsWithAttribute(XmlElement element, string[] localNames, string attributeName)
        {
            List<XmlElement> parentElements = new List<XmlElement>();
            parentElements.Add(element);
            int i = 0;
            for (i = 0; i < localNames.Length; i++)
            {
                string localName = localNames[i];
                List<XmlElement> foundElements = new List<XmlElement>();
                foreach (XmlElement parentElement in parentElements)
                {
                    foreach (XmlNode node in parentElement.ChildNodes)
                    {
                        if (node is XmlElement)
                        {
                            XmlElement currentElement = (XmlElement)node;
                            if (currentElement.LocalName.Equals(localName, StringComparison.Ordinal))
                            {
                                foundElements.Add(currentElement);
                            }
                        }
                    }
                }
                if (i == (localNames.Length - 1))
                {
                    foreach (XmlElement foundElement in foundElements)
                    {
                        XmlAttribute attr = foundElement.GetAttributeNode(attributeName);
                        if (attr != null)
                        {
                            return foundElement;
                        }
                    }
                    return null;
                }
                else
                {
                    parentElements = foundElements;
                }
            }
            return null;
        }
        //CID 0996 //2011.07.29 //End

        //CID 0996 //2011.07.29 //Begin
        public static XmlElement SelectChildElementByLocalNameWithAttributeValue(XmlElement element, string localName, string attributeName, string attributeValue)
        {
            foreach (XmlNode node in element.ChildNodes)
            {
                if (node is XmlElement)
                {
                    XmlElement currentElement = (XmlElement)node;
                    if (currentElement.LocalName.Equals(localName, StringComparison.Ordinal))
                    {
                        XmlAttribute attr = currentElement.GetAttributeNode(attributeName);
                        if (attr != null && attr.Value.Equals(attributeValue, StringComparison.Ordinal))
                        {
                            return currentElement;
                        }
                    }
                }
            }
            return null;
        }
        //CID 0996 //2011.07.29 //End

        //CID 0996 //2011.07.29 //Begin
        public static XmlElement SelectChildElementByLocalNameStepsWithAttributeValue(XmlElement element, string[] localNames, string attributeName, string attributeValue)
        {
            List<XmlElement> parentElements = new List<XmlElement>();
            parentElements.Add(element);
            int i = 0;
            for (i = 0; i < localNames.Length; i++)
            {
                string localName = localNames[i];
                List<XmlElement> foundElements = new List<XmlElement>();
                foreach (XmlElement parentElement in parentElements)
                {
                    foreach (XmlNode node in parentElement.ChildNodes)
                    {
                        if (node is XmlElement)
                        {
                            XmlElement currentElement = (XmlElement)node;
                            if (currentElement.LocalName.Equals(localName, StringComparison.Ordinal))
                            {
                                foundElements.Add(currentElement);
                            }
                        }
                    }
                }
                if (i == (localNames.Length - 1))
                {
                    foreach (XmlElement foundElement in foundElements)
                    {
                        XmlAttribute attr = foundElement.GetAttributeNode(attributeName);
                        if (attr != null && attr.Value.Equals(attributeValue, StringComparison.Ordinal))
                        {
                            return foundElement;
                        }
                    }
                    return null;
                }
                else
                {
                    parentElements = foundElements;
                }
            }
            return null;
        }
        //CID 0996 //2011.07.29 //End

        //CID 0996 //2011.08.12 //Begin
        public static XmlElement SearchChildElementByLocalName(XmlElement contextElement, string localName)
        {
            return SearchChildElementByLocalNameInner(contextElement, localName);
        }
        //CID 0996 //2011.08.12 //End

        //CID 0996 //2011.08.12 //Begin
        private static XmlElement SearchChildElementByLocalNameInner(XmlElement parentElement, string localName)
        {
            foreach (XmlNode childNode in parentElement.ChildNodes)
            {
                if (childNode is XmlElement)
                {
                    XmlElement element = childNode as XmlElement;
                    if (element.LocalName.Equals(localName, StringComparison.Ordinal))
                    {
                        return element;
                    }
                    else
                    {
                        XmlElement foundElement = SearchChildElementByLocalNameInner(element, localName);
                        if (foundElement != null)
                        {
                            return foundElement;
                        }
                    }
                }
            }
            return null;
        }
        //CID 0996 //2011.08.12 //End

        //CID 0996 //2011.07.30 //Begin
        public static List<XmlElement> SearchChildElementsByLocalName(XmlElement contextElement, string localName)
        {
            List<XmlElement> foundElements = new List<XmlElement>();
            SearchChildElementsByLocalName(contextElement, localName, foundElements);
            return foundElements;
        }
        //CID 0996 //2011.07.30 //End

        //CID 0996 //2011.07.30 //Begin
        private static void SearchChildElementsByLocalName(XmlElement parentElement, string localName, List<XmlElement> foundElements)
        {
            foreach (XmlNode childNode in parentElement.ChildNodes)
            {
                if (childNode is XmlElement)
                {
                    XmlElement element = childNode as XmlElement;
                    if (element.LocalName.Equals(localName, StringComparison.Ordinal))
                    {
                        foundElements.Add(element);
                        //CID 1016 //2012.01.19 //Begin
                        SearchChildElementsByLocalName(element, localName, foundElements);
                        //CID 1016 //2012.01.19 //End
                    }
                    else
                    {
                        SearchChildElementsByLocalName(element, localName, foundElements);
                    }
                }
            }
        }
        //CID 0996 //2011.07.30 //End

        //CID 0996 //2011.08.12 //Begin
        public static List<XmlElement> SearchChildElementsByLocalNames(XmlElement contextElement, string[] localNames)
        {
            List<XmlElement> foundElements = new List<XmlElement>();
            SearchChildElementsByLocalNames(contextElement, localNames, foundElements);
            return foundElements;
        }
        //CID 0996 //2011.08.12 //End

        //CID 0996 //2011.08.12 //Begin
        private static void SearchChildElementsByLocalNames(XmlElement parentElement, string[] localNames, List<XmlElement> foundElements)
        {
            foreach (XmlNode childNode in parentElement.ChildNodes)
            {
                if (childNode is XmlElement)
                {
                    XmlElement element = childNode as XmlElement;
                    foreach (string localName in localNames)
                    {
                        if (element.LocalName.Equals(localName, StringComparison.Ordinal))
                        {
                            foundElements.Add(element);
                            break;
                        }
                    }
                    //CID 1016 //2012.01.19 //Begin
                    SearchChildElementsByLocalNames(element, localNames, foundElements);
                    //CID 1016 //2012.01.19 //End
                }
            }
        }
        //CID 0996 //2011.08.12 //End

        //CID 0996 //2011.08.09 //Begin
        public static List<XmlElement> SearchElementsWithAttribute(XmlElement contextElement, string attrName)
        {
            List<XmlElement> elements = new List<XmlElement>();
            SearchElementsWithAttribute(contextElement, attrName, elements);
            return elements;
        }
        //CID 0996 //2011.08.09 //End

        //CID 0996 //2011.08.09 //Begin
        private static void SearchElementsWithAttribute(XmlElement contextElement, string attrName, List<XmlElement> elements)
        {
            if (contextElement.GetAttributeNode(attrName) != null)
            {
                elements.Add(contextElement);
            }
            foreach (XmlNode childNode in contextElement.ChildNodes)
            {
                if (childNode is XmlElement)
                {
                    XmlElement childElement = childNode as XmlElement;
                    SearchElementsWithAttribute(childElement, attrName, elements);
                }
            }
        }
        //CID 0996 //2011.08.09 //End

        //CID 0996 //2011.08.12 //Begin
        public static List<XmlElement> SearchElementsWithAttributeValue(XmlElement contextElement, string attrName, string attrValue)
        {
            List<XmlElement> elements = new List<XmlElement>();
            SearchElementsWithAttributeValue(contextElement, attrName, attrValue, elements);
            return elements;
        }
        //CID 0996 //2011.08.12 //End

        //CID 0996 //2011.08.12 //Begin
        private static void SearchElementsWithAttributeValue(XmlElement contextElement, string attrName, string attrValue, List<XmlElement> elements)
        {
            XmlAttribute attrNode = contextElement.GetAttributeNode(attrName);
            if (attrNode != null && attrNode.Value.Equals(attrValue, StringComparison.Ordinal))
            {
                elements.Add(contextElement);
            }
            foreach (XmlNode childNode in contextElement.ChildNodes)
            {
                if (childNode is XmlElement)
                {
                    XmlElement childElement = childNode as XmlElement;
                    SearchElementsWithAttributeValue(childElement, attrName, attrValue, elements);
                }
            }
        }
        //CID 0996 //2011.08.12 //End

        //CID 0996 //2011.08.13 //Begin
        public static List<XmlElement> SearchElementsByLocalNameWithAttributeValue(XmlElement contextElement, string localName, string attrName, string attrValue)
        {
            List<XmlElement> elements = new List<XmlElement>();
            SearchElementsByLocalNameWithAttributeValue(contextElement, localName, attrName, attrValue, elements);
            return elements;
        }
        //CID 0996 //2011.08.13 //End

        //CID 0996 //2011.08.13 //Begin
        private static void SearchElementsByLocalNameWithAttributeValue(XmlElement contextElement, string localName, string attrName, string attrValue, List<XmlElement> elements)
        {
            if (contextElement.LocalName.Equals(localName, StringComparison.Ordinal))
            {
                XmlAttribute attrNode = contextElement.GetAttributeNode(attrName);
                if (attrNode != null && attrNode.Value.Equals(attrValue, StringComparison.Ordinal))
                {
                    elements.Add(contextElement);
                }
            }
            foreach (XmlNode childNode in contextElement.ChildNodes)
            {
                if (childNode is XmlElement)
                {
                    XmlElement childElement = childNode as XmlElement;
                    SearchElementsByLocalNameWithAttributeValue(childElement, localName, attrName, attrValue, elements);
                }
            }
        }
        //CID 0996 //2011.08.13 //End

        //CID 0996 //2011.08.13 //Begin
        public static XmlElement SearchElementByLocalNameWithAttributeValue(XmlElement contextElement, string localName, string attrName, string attrValue)
        {
            return SearchElementByLocalNameWithAttributeValueInner(contextElement, localName, attrName, attrValue);
        }
        //CID 0996 //2011.08.13 //End

        //CID 0996 //2011.08.13 //Begin
        private static XmlElement SearchElementByLocalNameWithAttributeValueInner(XmlElement contextElement, string localName, string attrName, string attrValue)
        {
            if (contextElement.LocalName.Equals(localName, StringComparison.Ordinal))
            {
                XmlAttribute attrNode = contextElement.GetAttributeNode(attrName);
                if (attrNode != null && attrNode.Value.Equals(attrValue, StringComparison.Ordinal))
                {
                    return contextElement;
                }
            }
            foreach (XmlNode childNode in contextElement.ChildNodes)
            {
                if (childNode is XmlElement)
                {
                    XmlElement childElement = childNode as XmlElement;
                    XmlElement foundElement = SearchElementByLocalNameWithAttributeValueInner(childElement, localName, attrName, attrValue);
                    if (foundElement != null)
                    {
                        return foundElement;
                    }
                }
            }
            return null;
        }
        //CID 0996 //2011.08.13 //End

        //CID 0996 //2011.08.09 //Begin
        public static List<XmlAttribute> SearchAttributesByName(XmlElement contextElement, string attrName)
        {
            List<XmlAttribute> attributes = new List<XmlAttribute>();
            SearchAttributesByName(contextElement, attrName, attributes);
            return attributes;
        }
        //CID 0996 //2011.08.09 //End

        //CID 0996 //2011.08.09 //Begin
        private static void SearchAttributesByName(XmlElement contextElement, string attrName, List<XmlAttribute> attributes)
        {
            XmlAttribute attr = contextElement.GetAttributeNode(attrName);
            if (attr != null)
            {
                attributes.Add(attr);
            }
            foreach (XmlNode childNode in contextElement.ChildNodes)
            {
                if (childNode is XmlElement)
                {
                    XmlElement childElement = childNode as XmlElement;
                    SearchAttributesByName(childElement, attrName, attributes);
                }
            }
        }
        //CID 0996 //2011.08.09 //End
    }
}
